using System;
using System.Collections.Generic;
using System.Text;

using System.Web;
using System.IO;

namespace Pampa.Common.HTTPHandlers
{
    public class MediaHandler : BaseHandler
    {
        public MediaHandler() : base("Media")
        {
        }

        protected override void WriteContent(HttpContext context, Stream stream, string fileName)
        {
            // determino la extension del archivo
            int dotPos = fileName.LastIndexOf(".");
            string fileType = fileName.Substring(dotPos + 1, fileName.Length - dotPos - 1);

            try
            {
                BinaryReader br = new System.IO.BinaryReader(stream);

                context.Response.Clear();
                context.Response.ContentType = "image/" + this.fileType;
                context.Response.BufferOutput = true;

                byte[] buffer = new byte[1024];
                int count = 1024;
                while(count==1024)
                {
                    count = br.Read(buffer, 0, 1024);
                    context.Response.BinaryWrite(buffer);
                }
                context.Response.BinaryWrite(buffer);
                context.Response.Flush();
            }
            finally
            {
                stream.Close();
                stream.Dispose();
            }
        }
    }
}
